package com.lonely.fx.table_view;

import com.lonely.fx.table_view.entity.Student;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;

/**
 * @date: 2022-08-03 09:28
 * @author: ztkj-hzb
 * @description: 自定义单元格设置
 **/
public class CustomerTableCellSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        AnchorPane anchorPane = new AnchorPane();


        ObservableList<Student> persons = FXCollections.observableArrayList();
        persons.add(new Student("小天", "男", 0.25, false));
        persons.add(new Student("小地", "女", 0.5, Boolean.TRUE));
        persons.add(new Student("小玄", "男", 0.75, false));
        persons.add(new Student("小黄", "女", 1.0, true));

        TableView<Student> tableView = new TableView<>(persons);

        tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

        tableView.setPrefSize(400, 400);


        TableColumn<Student, String> nameTableCol = new TableColumn<>("学生名称");
        TableColumn<Student, String> sexTableCol = new TableColumn<>("性别");
        TableColumn<Student, Double> scoreTableCol = new TableColumn<>("成绩");
        TableColumn<Student, Boolean> passTableCol = new TableColumn<>("是否及格");

        nameTableCol.setCellValueFactory(new PropertyValueFactory<>("name"));
        sexTableCol.setCellValueFactory(new PropertyValueFactory<>("sex"));
        scoreTableCol.setCellValueFactory(new PropertyValueFactory<>("score"));
        passTableCol.setCellValueFactory(param -> param.getValue().getPassProperty());

        //passTableCol.setCellFactory(CheckBoxTableCell.forTableColumn(passTableCol));

        tableView.getColumns().addAll(nameTableCol, sexTableCol, scoreTableCol, passTableCol);
        anchorPane.getChildren().addAll(tableView);
        AnchorPane.setTopAnchor(tableView, 100.0);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(800);
        primaryStage.setHeight(700);
        primaryStage.show();


        //设置可编辑
        tableView.setEditable(true);

        //TODO 自定义单元格样式
        Image image = new Image("icons/pkq.png", 30, 30, true, true);
        Image image2 = new Image("icons/mla.png", 30, 30, true, true);
        sexTableCol.setCellFactory(new Callback<TableColumn<Student, String>, TableCell<Student, String>>() {
            @Override
            public TableCell<Student, String> call(TableColumn<Student, String> param) {
                return new TableCell<Student, String>() {
                    /**
                     *
                     */
                    @Override
                    protected void updateItem(String item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            return;
                        }

                        HBox hBox = new HBox();
                        ImageView imageView = new ImageView();

                        if ("男".equals(item)) {
                            imageView.setImage(image);
                        } else {
                            imageView.setImage(image2);
                        }

                        hBox.getChildren().addAll(imageView);
                        hBox.setAlignment(Pos.CENTER);

                        this.setGraphic(hBox);


                    }
                };
            }
        });


        scoreTableCol.setCellFactory(new Callback<TableColumn<Student, Double>, TableCell<Student, Double>>() {
            @Override
            public TableCell<Student, Double> call(TableColumn<Student, Double> param) {
                return new TableCell<Student, Double>() {
                    /**
                     *
                     */
                    @Override
                    protected void updateItem(Double item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            return;
                        }

                        HBox hBox = new HBox();

                        ProgressIndicator progressIndicator = new ProgressIndicator();
                        //progressIndicator.setPrefSize(30, 30);
                        progressIndicator.setProgress(item);

                        hBox.getChildren().addAll(progressIndicator);
                        hBox.setAlignment(Pos.CENTER);

                        this.setGraphic(hBox);

                    }
                };
            }
        });


        passTableCol.setCellFactory(new Callback<TableColumn<Student, Boolean>, TableCell<Student, Boolean>>() {
            @Override
            public TableCell<Student, Boolean> call(TableColumn<Student, Boolean> param) {
                return new TableCell<Student, Boolean>() {
                    /**
                     *
                     */
                    @Override
                    protected void updateItem(Boolean item, boolean empty) {
                        super.updateItem(item, empty);

                        if (empty) {
                            return;
                        }

                        HBox hBox = new HBox();

                        //选择框和pass值双向绑定
                        CheckBox checkBox = new CheckBox();
                        int index = this.getTableRow().getIndex();
                        checkBox.selectedProperty().bindBidirectional(tableView.getItems().get(index).getPassProperty());

                        hBox.getChildren().addAll(checkBox);
                        this.setGraphic(hBox);


                        checkBox.setOnAction(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                tableView.getItems().stream().forEach(x -> System.out.println(String.format("%s--%s", x.getName(), x.getPass())));
                            }
                        });
                    }
                };
            }
        });

    }
}
